//
//  HomeViewController.m
//  植物管理
//
//  Created by liujianlong on 2022/11/21.
//

#import "HomeViewController.h"
#import "MineViewController.h"
#import "AddViewController.h"
#import "HomeCell.h"
#import "CameraViewController.h"
#import "DetailViewController.h"

@interface HomeViewController ()
@property (strong, nonatomic) IBOutlet UIButton *addBtn;
@property (strong, nonatomic) IBOutlet UIImageView *nodataImg;
@property (strong, nonatomic) NSMutableArray <Job *>*array;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loaddata];
    [NotifyTool add:self :@selector(loaddata) :notifyJobUpdate];
}
- (void)initUI{
    self.title = LS(@"我的植物");
    [self.backBtn setImage:[UIImage imageNamed:@"首页设置"] forState:UIControlStateNormal];
    self.tableView.c_edges.equalTo(self.view);
    [self.tableView registerNib:[HomeCell nib] forCellReuseIdentifier:[HomeCell ID]];
    self.nodataImg.image = [UIImage imageNamed:LS(@"无数据")];
    [self.view bringSubviewToFront:self.addBtn];
}
- (void)loaddata{
    [JobTool find:^(NSMutableArray<Job *> * _Nonnull result) {
        self.array = [NSMutableArray arrayWithArray:result];
        self.nodataImg.hidden = self.array.count > 0;
        [self.tableView reloadData];
    }];
}
- (void)backBtnAction{
    MineViewController *vc = [MineViewController new];
    [self push:vc :YES];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    SheetViewController *sheetvc = [SheetViewController new];
    sheetvc.array = @[@"拍照识别",@"手动录入",@"取消"];
    WS(sheetvc)
    WS(self)
    sheetvc.block = ^(int index, NSString * _Nonnull text) {
        if ([text isEqualToString:@"取消"]) {
            return [weaksheetvc dismissViewControllerAnimated:NO completion:nil];
        }else if ([text isEqualToString:@"拍照识别"]){
            NSString *mediaType = AVMediaTypeVideo; //读取媒体类型
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; //读取设备授权状态
            if (authStatus == AVAuthorizationStatusNotDetermined || authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) { // 授权受阻
                UIAlertController *alterCon = [UIAlertController alertControllerWithTitle:LS(@"是否开启相机访问权限？")
                                                                                  message:nil
                                                                           preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *OFF = [UIAlertAction actionWithTitle:LS(@"取消") style:UIAlertActionStyleDefault handler:nil];
                UIAlertAction *ON = [UIAlertAction actionWithTitle:LS(@"确定") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { // 设置权限
                    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    if ([[UIApplication sharedApplication] canOpenURL:url]) {
                        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                            // do something
                        }];
                    }
                }];
                [alterCon addAction:OFF];
                [alterCon addAction:ON];
                [self presentViewController:alterCon animated:YES completion:nil];
            } else { // 已开启
                CameraViewController *vc = [CameraViewController new];
                vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakself presentViewController:vc animated:YES completion:nil];
            }
        }else if ([text isEqualToString:@"手动录入"]){
            AddViewController *vc = [AddViewController new];
            [weakself push:vc :YES];
        }
    };
    [self presentViewController:sheetvc animated:NO completion:nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeCell ID]];\
    cell.model = self.array[indexPath.row];
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *vc = [DetailViewController new];
    vc.model = self.array[indexPath.row];
    [self push:vc :YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return  nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
}
@end
